Fix read_vrt leaking integer source nodata in float-dataType VRT (#1616)#1621
Merged
brendancol merged 2 commits intoMay 11, 2026
Merged
Conversation
A VRT declaring ``dataType="Float32"`` or ``"Float64"`` over integer source GeoTIFFs used to leak the GDAL_NODATA sentinel value as a literal float (e.g. 65535.0) into the returned array. ``attrs['nodata']`` advertised the sentinel but NaN-aware downstream code (``np.isnan``, ``xr.where``, slope/aspect, statistics) saw the sentinel pixels as valid data and produced wrong answers. The eager numpy, dask, and GPU paths all funnel through ``_vrt.read_vrt``, which only NaN-masked source arrays whose dtype was already float. Mask integer source arrays too when the result buffer is float, gated on the sentinel being representable in the source dtype so out-of-range values (uint16 file + ``NoDataValue=-9999``) stay no-op rather than tripping ``OverflowError``. Single-file integer rasters already get the sentinel-to-NaN promotion from the wrappers in ``__init__.py``; this fix brings the VRT path in line with that semantics for the int-source + float-dataType combo.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a VRT-reading nodata masking bug where integer-source GDAL_NODATA sentinels leaked into float-typed VRT outputs as literal finite floats (e.g., 65535.0) instead of being converted to NaN, which broke downstream NaN-aware processing.
Changes:
- Add an integer-source + float-VRT nodata masking branch in
xrspatial/geotiff/_vrt.pyto prevent sentinel leakage. - Add a dedicated regression test module covering multiple dtype/sentinel scenarios, dask wrapping, and per-band selection.
- Update internal
.claude/sweep-metadata-state.csvtracking entry for issue #1616.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
xrspatial/geotiff/_vrt.py |
Adds masking logic for integer-source arrays when the VRT declares a floating output dtype. |
xrspatial/geotiff/tests/test_vrt_int_source_float_dtype_1616.py |
Adds regression coverage for the int-source + float-VRT nodata leak, including dask and band selection. |
.claude/sweep-metadata-state.csv |
Updates metadata tracking to reflect issue #1616. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+399
to
+403
| sentinel = src_arr.dtype.type(nodata_int) | ||
| mask = src_arr == sentinel | ||
| if mask.any(): | ||
| src_arr = src_arr.astype(result.dtype) | ||
| src_arr[mask] = result.dtype.type('nan') |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
dataType="Float32"or"Float64"over integer source GeoTIFFs leaked theGDAL_NODATAsentinel as a literal float into the returned array (e.g.65535.0instead ofnp.nan).attrs['nodata']advertised the sentinel but NaN-aware downstream code saw the sentinel pixels as valid data._vrt.read_vrtnow NaN-masks integer source arrays before they're placed into a floatresultbuffer, gated on the sentinel being representable in the source dtype so out-of-range values stay no-op rather than trippingOverflowError.__init__.pypromotion) and the float-source case (already handled inline by_vrt.read_vrt).Test plan
test_vrt_int_source_float_dtype_1616.pycoversFloat32 + uint16,Float64 + int16, the out-of-range-sentinel no-op, the no-pixel-match case, dask wrapper,attrs['nodata']round-trip, and per-bandband=Nselection (7 tests, all pass).test_vrt_band_nodata_1598,test_vrt_int_nodata_1564,test_vrt_multiband_int_nodata_1611,test_vrt_write,test_vrt_tiled_metadata_1606,test_vrt_xml_escape_1607).TestPalettematplotlibdeepcopyfailures (unrelated to this PR).